package org.jeecg.glr.iframe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.SysConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SysUtil;
import org.jeecg.glr.entity.GlrVideo;
import org.jeecg.glr.entity.GlrVideoBdRecognize;
import org.jeecg.glr.iframe.service.IGlrIframeVideoMonitoringService;
import org.jeecg.glr.iframe.util.AjaxJson;
import org.jeecg.glr.service.IGlrVideoBdRecognizeService;
import org.jeecg.glr.service.IGlrVideoService;
import org.jeecg.glr.service.IGlrVideoYsService;
import org.jeecg.glr.util.HttpRequestUtil;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.jeecg.common.constant.CommonConstant.St_Access_Token;
import static org.jeecg.glr.util.GLRConstant.FACE_RECOGNITION_RATE;

/**
 *iframe使用
 * 视频播放功能
 */
@Slf4j
@Controller
@RequestMapping("/iframeGlr/faceMap")
public class GlrIframeFaceMap extends JeecgController<GlrVideo, IGlrVideoService> {

    @Autowired
    private IGlrVideoService glrVideoService;

    @Autowired
    private IGlrVideoBdRecognizeService glrVideoBdRecognizeService;

    @Autowired
    private IGlrIframeVideoMonitoringService glrIframeVideoMonitoringService;

    @Value(value = "${glr.path.viewBmpPath}")
    private String viewBmpPath;

    @Value("${glr.path.vue_url}")
    private String vue_url;

    /**
     * 人脸地图
     * @param modelAndView
     * @return
     */
    @RequestMapping("/index")
    public String list(Model model, HttpServletRequest request) throws Exception {
        try{
            LoginUser loginUser=glrIframeVideoMonitoringService.updateUserInfo(request);
            if (loginUser==null){
                return "redirect:"+vue_url;
            }
        }catch (Exception ex){
            return "redirect:"+vue_url;
        }
        model.addAttribute("AccessToken",request.getParameter(St_Access_Token));
        return "/glrTemplates/FaceMap/index";
    }

    //getUserInfo
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public AjaxJson getUserInfo(HttpServletRequest request, HttpServletResponse response,String equipmentSerial, String userInfo, String executeTime, String score){
        AjaxJson j = new AjaxJson();
        try{
            LoginUser loginUser= glrIframeVideoMonitoringService.updateUserInfo(request);

            QueryWrapper<GlrVideoBdRecognize> queryWrapper = new QueryWrapper<GlrVideoBdRecognize>();
            if(loginUser!=null){
                //获取用户当前角色
                String userType= loginUser.getUserType();
                if(!userType.equals(SysConstant.SYS_USER_TYPE_ADMIN)&&!userType.equals(SysConstant.SYS_USER_TYPE_0)){
                    queryWrapper.eq(SysConstant.SYS_TABLE_COMPANY_CODE,loginUser.getSysCompanyCode());
                }
            }

            if (StringUtil.isNotEmpty(equipmentSerial)){
                queryWrapper.like("equipment_serial",equipmentSerial);
            }
            if (StringUtil.isNotEmpty(userInfo)){
                queryWrapper.like("user_info",userInfo);
            }
            executeTime=executeTime.replace("-","");
            if (StringUtil.isNotEmpty(executeTime)){
                queryWrapper.like("take_photo_time_string",executeTime);
            }

            if  (!StringUtil.isNotEmpty(score)){
                queryWrapper.ge("score", FACE_RECOGNITION_RATE);
            }else {
                if (StringUtil.isFloatNumeric(score)){
                    double d1=Double.parseDouble(score);
                    queryWrapper.ge("score", d1);
                }else{
                    queryWrapper.ge("score", 0d);
                }
            }
            queryWrapper.orderByAsc("take_photo_time_string");

            List<GlrVideoBdRecognize> list=glrVideoBdRecognizeService.list(queryWrapper);
            java.text.SimpleDateFormat s= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            List<GlrVideoBdRecognize> list1 =new ArrayList<GlrVideoBdRecognize>();
            for (GlrVideoBdRecognize entity : list){
                String stcatalog=entity.getCatalog();
                String stequipmentSerial=entity.getEquipmentSerial();

                QueryWrapper<GlrVideo> queryWrapper1 = new QueryWrapper<GlrVideo>();
                queryWrapper1.eq("equipment_upload_code",stequipmentSerial);

                GlrVideo domain=glrVideoService.getOne(queryWrapper1);

                boolean bset=false;
                if (domain!=null){
                    if (domain.getLatitude()!=null){
                        entity.setLat(domain.getLatitude());
                        entity.setLan(domain.getLongitude());
                        bset=true;
                    }
                }
                if (!bset){
                    entity.setLat("34.1607962589");
                    entity.setLan("117.1778350764");
                }

                String sttakePhotoTimeString=entity.getTakePhotoTimeString();
                //String st1=stcatalog+"/hk__"+stequipmentSerial+"_"+sttakePhotoTimeString+"_FACE_SNAP.jpg";
                entity.setSysOrgCode(viewBmpPath+"/sys/common/view/"+stcatalog);

                String sttemp=sttakePhotoTimeString.substring(0,4)+"-"+sttakePhotoTimeString.substring(4,6)+"-"+sttakePhotoTimeString.substring(6,8)+" "+sttakePhotoTimeString.substring(8,10)+":"+sttakePhotoTimeString.substring(10,12)+":"+sttakePhotoTimeString.substring(12,14);
                entity.setSysCompanyCode( sttemp);

                //entity.setSysCompanyCode( s.format(entity.getTakePhotoTimeDate()));

                boolean bfind=false;
                for(GlrVideoBdRecognize entity1 : list1){
                    if (entity1.getEquipmentSerial().equals(entity.getEquipmentSerial())){
                        bfind=true;
                        break;
                    }
                }

                if (!bfind) list1.add(entity);

            }
            j.setObj(list1);
            j.setSuccess(true);

        }catch (Exception e){
            j.setSuccess(false);
            e.printStackTrace();
        }
        return j;
    }


}
